<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="根据描述搜索">
            <a-input v-decorator="['des']" placeholder="请输入商品名称" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
          <!-- <a-form-item class="search-btn">
            <a-button @click="handleReset">重置</a-button>
          </a-form-item>-->
        </a-form>
      </a-row>
      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        <div class="tab-list fl-l">
          <a-radio-group :defaultValue="queryParam.status" @change="handleTabs">
            <a-radio-button value="1">正常</a-radio-button>
            <a-radio-button value="0">禁用</a-radio-button>
          </a-radio-group>
        </div>
        <a-button
          v-if="$auth('/market/wine/perform')"
          class="fl-l"
          type="primary"
          icon="plus"
          @click="handleCreate"
        >创建酒业业绩</a-button>
        <div v-if="selectedRowKeys.length" class="button-group">
          <a-button-group class="ml-10">
            <a-button icon="arrow-up" @click="handleUpdateStatus(selectedRowKeys, 0)">启用</a-button>
            <a-button icon="arrow-down" @click="handleUpdateStatus(selectedRowKeys, 1)">禁用</a-button>
            <a-button icon="delete" @click="handleDelete(selectedRowKeys)">删除</a-button>
          </a-button-group>
        </div>
      </div>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :rowSelection="rowSelection"
      :pageSize="15"
    >
      <!-- 商品名称 -->
      <span slot="award" slot-scope="text">
        <p class="twoline-hide">{{ text }}</p>
      </span>
      <!-- 商品状态 -->
      <span slot="status" slot-scope="text, item">
        <a-tag
          class="cur-p"
          :color="text == 1 ? 'green' : 'red'"
          @click="handleUpdateStatus([item.id], text)"
        >{{ text == 1 ? '正常' : '禁用' }}</a-tag>
      </span>
      <!-- 操作项 -->
      <div class="actions" slot="action" slot-scope="text, item">
        <a v-if="$auth('/goods/update')" @click="handleEdit(item)">编辑</a>
        <a v-action:delete @click="handleDelete([item.id])">删除</a>
      </div>
    </s-table>
  </a-card>
</template>

<script>
import * as Api from '@/api/market/wine/perform'
import { ContentHeader, STable } from '@/components'

// 表格表头
const columns = [
  {
    title: 'ID',
    dataIndex: 'id'
  },
  {
    title: '奖励百分比',
    dataIndex: 'award',
    scopedSlots: { customRender: 'award' }
  },
  {
    title: '业绩金额',
    dataIndex: 'performance',
    scopedSlots: { customRender: 'performance' }
  },
  {
    title: '描述',
    dataIndex: 'des',
    scopedSlots: { customRender: 'des' }
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '添加时间',
    dataIndex: 'create_time'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' }
  }
]


export default {
  name: "PerformIndex",
  components: {
    ContentHeader,
    STable
  },
  data () {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 商品分类列表
      categoryListTree: [],
      // 查询参数
      queryParam: {
        status: '1',
        limit: '15'
      },
      // 正在加载
      isLoading: false,
      // 表头
      columns,
      // 选择的元素
      selectedRowKeys: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.getList({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  created () {
    // 默认的查询参数
    if (this.$route.query.listType) {
      this.queryParam.listType = this.$route.query.listType
    }
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    /**
     * 选中项发生变化时的回调
     */
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    // 切换tab
    handleTabs (e) {
      this.queryParam.status = e.target.value
      this.handleRefresh(true)
    },

    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    // 重置搜索表单
    handleReset () {
      this.searchForm.resetFields()
    },
    // 修改商品状态(正常，禁用)
    handleUpdateStatus (id, status = true) {
      this.isLoading = true
      Api.setStatus({ id, status })
        .then(result => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          this.handleRefresh()
        })
        .finally(result => { this.isLoading = false })
    },

    /**
     * 删除记录
     */
    handleDelete (id) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.del({ id })
            .then(result => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 新增记录
     */
    handleCreate () {
      this.$router.push('/market/wine/addperform')
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      this.$router.push({ path: '/market/wine/editperform', query: { id: item.id } })
    },

    /**
     * 刷新列表
     * @param bool bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.selectedRowKeys = []
      this.$refs.table.refresh(bool)
    }
  }
}
</script>

<style scoped>

</style>